import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../services/screen_adapter.dart';
import '../../provider/cart.dart';

class CartNum extends StatefulWidget {

  final Map _itemData;

  CartNum(this._itemData, {Key key}) : super(key: key);

  @override
  _CartItemState createState() => _CartItemState();
}

class _CartItemState extends State<CartNum> {

  Map _itemData;

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init();
    /// 注意：initSetData里只是初始化的时候赋值，重新build时不再赋值了
    _itemData = widget._itemData;
    return Container(
      width: ScreenAdapter.width(168),
      decoration: BoxDecoration(
        border: Border.all(
          width: ScreenAdapter.width(2),
          color: Colors.black12
        )
      ),
      child: Row(
        children: <Widget>[
          _leftBtn(),
          _centerArea(),
          _rightBtn()
        ]
      )
    );
  }

  /// 左侧按钮
  Widget _leftBtn() {
    return InkWell(
      onTap: (){
        if(_itemData['count'] > 1){
          _itemData['count']--;
          context.read<Cart>().itemCountChange();
        }
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenAdapter.width(45),
        height: ScreenAdapter.height(45),
        child: Text('-')
      )
    );
  }

  /// 右侧按钮
  Widget _rightBtn() {
    return InkWell(
      onTap: (){
        _itemData['count']++; 
        context.read<Cart>().itemCountChange();
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenAdapter.width(45),
        height: ScreenAdapter.height(45),
        child: Text('+')
      )
    );
  }

  /// 中间区域
  Widget _centerArea() {
    return Container(
      alignment: Alignment.center,
      width: ScreenAdapter.width(70),
      height: ScreenAdapter.height(45),
      decoration: BoxDecoration(
        border: Border(
          left: BorderSide(
            width: ScreenAdapter.width(2),
            color: Colors.black12
          ),
          right: BorderSide(
            width: ScreenAdapter.width(2),
            color: Colors.black12
          )
        )
      ),
      child: Text('${_itemData['count']}')
    );
  }
}